//
//  HometwoViewController.m
//  SinceLe
//
//  Created by 李元喜 on 15/11/24.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import "HometwoViewController.h"
#import "HometwoCell.h"
#import "headerView.h"
#import "netWorkHandle.h"
#import "MusicModel.h"
#import "Macrocs.h"
#import <UIImageView+WebCache.h>
@interface HometwoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) headerView *headerView;
@property (nonatomic, strong) UIView *oneview;
@property (nonatomic, strong) NSMutableArray *dataArraytwo;
@end

@implementation HometwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadTableView];
//  [self loadHometwoData];
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)loadTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _headerView  = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil] firstObject];
    _headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
    _tableView.tableHeaderView = _headerView;
    
    UINib *nib = [UINib nibWithNibName:@"HometwoCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
  
    [self.view addSubview:_tableView];
    
}



//- (void)requestAllMusicModelsWithUrlString:(NSString *)urlString didFinished:(BLOCK)result
//{
//    // 网络请求, 封装model对象
//    // 1.网络请求
//    // 防止循环引用
//    __weak MusicModel *dataHelper = self;
//    
//    // GCD开启一个线程来请求数据(因为正常请求数据是在主线程中) --->在线程中创建一个全局队列
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        // 同步请求数据
//        NSURL *url = [NSURL URLWithString:urlString];
//        NSArray *array = [NSArray arrayWithContentsOfURL:url];
//        NSLog(@"%@", array);
//        // 初始化数组
//        dataHelper.allMusicModelsArray = [NSMutableArray array];
//        // 遍历数组, 将有效信息封装成Model对象
//        for (NSDictionary *dic in array) {
//            // 使用KVC封装Model对象
//            MusicModel *musci = [[MusicModel alloc] init];
//            [musci setValuesForKeysWithDictionary:dic];
//            [dataHelper.allMusicModelsArray addObject:musci];
//        }
//        
//        NSLog(@"%@", self.allMusicModelsArray);
//        
//        // 刷新表视图 由主线程操作(从子线程调到主线程里)
//        dispatch_async(dispatch_get_main_queue(), ^{
//            // block调用
//            result(_allMusicModelsArray);
//        });
//    });
//}









////数据解析
//-(void)loadHometwoData{
//    //_id.传值到详细界面
//    
//    NSLog(@"id -=-=-   %ld", (long)_ID);
//    NSString *ss = [NSString stringWithFormat:@"http://graph.luoo.net/vol/detail?id=%d&platform=ios&version=4.2.0&key=6cc95158f3c3c11b884a3eeb58203392", _ID];
//    
//    
////    NSString *Str = [NSString stringWithFormat:KhomeUrl,_name];
//    NSLog(@"----   %@", ss);
//    http://graph.luoo.net/vol/detail?id=793&platform=ios&version=4.2.0&key=6cc95158f3c3c11b884a3eeb58203392
//    [netWorkHandle getDataWithURLString:ss compare:^(id object) {
//        NSLog(@"p=pp=p=pppp======   %@", object);
//        _dataArraytwo = [NSMutableArray array];
//        _dataArray = [NSMutableArray array];
//        
//        NSDictionary *dic = object;
//        NSDictionary* dicc = [NSDictionary dictionary];
//        dicc = dic[@"data"];
//        
//        //对头部view进行kvc
//        MusicModel* headModel = [[MusicModel alloc] init];
//        [headModel setValuesForKeysWithDictionary:dicc];
//        _headerView.MusicModel = headModel;
//        
//        
//        NSLog(@"_dataArray====11%@",_dataArray);
//        NSArray *arr = dic[@"data"][@"songs"];
//        
//        for (NSDictionary *a in arr) {
//            MusicModel *model = [[MusicModel alloc]init];
//            [model setValuesForKeysWithDictionary:a];
//            
//            [_dataArraytwo addObject:model];
//        }
//        NSLog(@"_dataArraytwo2222 ====%@",_dataArraytwo);
//        
//        
//        [_tableView reloadData];
//    }];
//    
//}



//cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}

//返回多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArraytwo.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    HometwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   
//赋值
    MusicModel * model = _dataArraytwo[indexPath.row];
    [cell.twocellimage sd_setImageWithURL:[NSURL URLWithString:model.imageurl]];
    cell.twocellname.text = model.name;
    cell.twooneltitle.text = model.artist;
    
    
     return cell;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
